package Server.controller;

import Plug.operLog.OperLog;
import Plug.result.RetResponse;
import Plug.result.RetResult;
import Server.dao.CityDao;
import Server.dao.UsersCityDao;
import Server.dao.UsersDao;
import Server.entity.City;
import Server.entity.InviteUsers;
import Server.entity.Users;
import Server.entity.UsersCity;
import Server.tool.BeanToJson;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedList;
import java.util.List;

@RestController
@RequestMapping("LeaderController")
public class LeaderController {
    @Autowired
    private UsersCityDao usersCityDao;
    @Autowired
    private CityDao cityDao;
    @Autowired
    private UsersDao usersDao;
    @RequestMapping("List")
    public RetResult List(@RequestBody JSONObject jsonObject){
        UsersCity entity = (UsersCity) BeanToJson.jsonTobean(jsonObject.getJSONObject("common").toString(), UsersCity.class);
        int page = Integer.parseInt(jsonObject.getString("page"))-1;
        int size = Integer.parseInt(jsonObject.getString("limit"));
        Users fspuser = (Users) SecurityUtils.getSubject().getSession().getAttribute("Users");
        fspuser=usersDao.findByUuid(fspuser.getUuid());
        Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, "uuid");
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("city.city", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("users.name", ExampleMatcher.GenericPropertyMatchers.contains());
        Example<UsersCity> example = Example.of(entity,matcher);
        Page<UsersCity> userlist = usersCityDao.findAll(example,pageable);
        return RetResponse.makeRsp(0, "成功", userlist.getContent(), (int) userlist.getTotalElements());
    }
    @RequestMapping("CityList")
    public RetResult CityList(@RequestBody JSONObject jsonObject){
        City entity = (City) BeanToJson.jsonTobean(jsonObject.getJSONObject("common").toString(), City.class);
        int page = Integer.parseInt(jsonObject.getString("page"))-1;
        int size = Integer.parseInt(jsonObject.getString("limit"));
        Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, "uuid");
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("city", ExampleMatcher.GenericPropertyMatchers.contains());
        Example<City> example = Example.of(entity,matcher);
        Page<City> userlist = cityDao.findAll(example,pageable);
        return RetResponse.makeRsp(0, "成功", userlist.getContent(), (int) userlist.getTotalElements());
    }
    @OperLog("新增业务员辖区")
    @RequestMapping("AddUserCity")
    public RetResult AddUserCity(@RequestBody UsersCity usersCity){
        try {
            if(StringUtils.isEmpty(usersCity.getUuid())){
                /*if(usersCityDao.findByUsers_Uuid(usersCity.getUsers().getUuid())!=null){
                    return RetResponse.makeErrRsp("用户已有辖区，不允许增加！");
                }*/
                if(usersCityDao.findByCity_Uuid(usersCity.getCity().getUuid())!=null){
                    return RetResponse.makeErrRsp("辖区已有业务员，不允许增加！");
                }
                usersCity.setCity(cityDao.findByUuid(usersCity.getCity().getUuid()));
                usersCity.setUsers(usersDao.findByUuid(usersCity.getUsers().getUuid()));
            }else{
                if(usersCityDao.findByCity_Uuid(usersCity.getCity().getUuid())!=null){
                    return RetResponse.makeErrRsp("辖区已有业务员，不允许增加！");
                }
                UsersCity entity = usersCityDao.findByUuid(usersCity.getUuid());
                entity.setCity(cityDao.findByUuid(usersCity.getCity().getUuid()));
                usersCity=entity;
            }
            usersCityDao.save(usersCity);
            return RetResponse.makeOKRsp();
        }catch (Exception e){
            return RetResponse.makeErrRsp("辖区已有业务员！");
        }
    }
    @Transactional
    @OperLog("删除业务员辖区")
    @RequestMapping("DelUserCity")
    public RetResult DelUserCity(@RequestBody UsersCity usersCity){
        usersCityDao.deleteByUuid(usersCity.getUuid());
        return RetResponse.makeOKRsp();
    }
}
